package com.example.day09.exercise;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class SnakeGame {

    private  static  final  int ROW=20;
    private  static  final  int COULUS=20;

    private  Snake snake;


    public Set<Node> foods=new HashSet<>();
     private  LinkedList<Node> list=new LinkedList<>();

    public SnakeGame() {

        this.snake=new Snake();
        initFod(3);
    }

    public Snake getSnake() {
        return snake;
    }

    private void initFod(int count) {
        while (foods.size()<count){
            int i= (int) (Math.random()*18+1);
            int j= (int) (Math.random()*18+1);

            if(snake.containNode(i,j)){
                continue;
            }

            foods.add(new Node(i,j));

        }

    }



    public   void start(){
        for(int i=0;i<ROW;i++){
            for (int j=0;j<COULUS;j++){
                if(i==0||i==ROW-1||j==0||j==COULUS-1){
                    System.out.printf("%2s","*");
                }else  if(snake.containNode(i,j)){
                    System.out.printf("%2s","#");

                }else  if(foods.contains(new Node(i,j))){
                    System.out.printf("%2s","0");

                }
                else{
                    System.out.printf("%2s"," ");
                }
            }
            System.out.println();
        }
    }
    public class Snake{

        private LinkedList<Node> snake=new LinkedList<>();
        public  static  final   int LEFT=-1;
        public  static  final   int RIGHT=1;
        public static  final   int DOWN=-2;
        public  static  final   int UP=2;

        private  int dir;

        public Snake() {
            snake.add(new Node(5,5));
            snake.add(new Node(5,6));
            snake.add(new Node(5,7));
            dir=LEFT;
        }

        public   boolean containNode(int x,int y){
            return  snake.contains(new Node(x,y));
        }
        public  void  run(int dir){
            if(this.dir+dir==0){
                throw  new RuntimeException("sorry,不能反方向执行！Game  Over ~~~");
            }
            this.dir=dir;

            run();
        }

        public  void  run(){
            //获取蛇头
            Node  header=snake.getFirst();
            int i= header.getX();
            int j= header.getY();
            switch (dir){
                case  UP:
                    i--;
                    break;
                case  DOWN:
                    i++;
                    break;
                case  LEFT:
                    j--;
                    break;
                case RIGHT:
                    j++;
                    break;
            }

            //新的节点
            header=new Node(i,j);

            //判断蛇身是否包含header
            if(containNode(i,j)){
                throw  new RuntimeException("sorry，撞到自己！Game Over ~~");
            }

            //将这个新的节点放入到蛇头
            snake.addFirst(header);

            /**
             * 判断 header是否属于事务的节点
             * 如果属于食物的节点
             * 1.删除尾巴
             * 2.删除食物
             */
            if(foods.remove(header)){  //成功删除，返回true
                return;
            }

            //删除结点
            snake.removeLast();



        }
    }



}

class      Main{
    public static void main(String[] args) {

        Scanner scanner=new Scanner(System.in);
        SnakeGame snakeGame=new SnakeGame();
        SnakeGame.Snake snake=snakeGame.getSnake();

        do {
            snakeGame.start();
            System.out.println("w->上 s->下 a->左  d->右");
            String  dir=scanner.nextLine();
            switch (dir){
                case "w":
                    snake.run(SnakeGame.Snake.UP);
                    break;
                case "s":
                    snake.run(SnakeGame.Snake.DOWN);
                    break;
                case "a":
                    snake.run(SnakeGame.Snake.LEFT);
                    break;
                case "d":
                    snake.run(SnakeGame.Snake.RIGHT);
                    break;
                default:
                    snake.run();
            }

            if (snakeGame.foods.size()==0){
                System.out.println("恭喜您过关了！");
                snakeGame.start();
                break;
            }
        }while (true);


    }
}
